﻿namespace SKeeper.Application.ViewModels.Invoice
{
    using System;
    using System.Linq;
    using System.Collections.Generic;

    using SKeeper.Domain.Invoices;
    using Infrastructure;
    using Items;

    public class InvoiceViewModel
    {
        public int Id { get; set; }

        public string Code { get; set; }

        public int WarehouseId { get; set; }

        public string WarehouseName { get; set; }

        public IEnumerable<ItemMovementViewModel> Items { get; set; }

        public IEnumerable<ItemViewModel> CompanyItems { get; set; }

        public DateTime CreateDate { get; set; }

        public DateTime DueDate { get; set; }

        public bool IsActive { get; set; }

        public InvoiceType InvoiceType { get; private set; }
        
        public static InvoiceViewModel FromInvoice(Invoice invoice)
        {
            return new InvoiceViewModel
            {

                Id = invoice.Id,
                CreateDate = invoice.CreationDate,
                DueDate = invoice.DueDate,
                IsActive = invoice.IsActive,
                WarehouseId = invoice.WarehouseId,
                WarehouseName = invoice.Warehouse.Name,
                Items = invoice.ItemsMovement.Select(x => ItemMovementViewModel.FromStoredItem(x))
            };
        }

        internal Invoice ToInvoice()
        {
            return new Invoice
            {
                Code = this.Code,
                CreationDate = DateTime.Now,
                DueDate = this.DueDate,
                Id = this.Id,
                InvoiceType = this.InvoiceType,
                IsActive = this.IsActive,
                WarehouseId = this.WarehouseId
            };
        }
    }
}
